/*****************************************************

  Copyright:	(c) 2006  SeaDesign Web Studio
        Web:	www.seadesign.ru
      Email: 	seadesign@gmail.com
  
*****************************************************/

function go(url)
{
	if (arguments.length < 2) {
		if (document.images) {
			location.replace(url);
		}
		else {
			location.href = url;
		}
		return;
	}
	
	var oForm = document.createElement('FORM');
	oForm.style.visibility = 'hidden';
	oForm.action = url;
	oForm.method = 'POST';
	document.body.appendChild(oForm);
	
	for (var i=1; i<arguments.length; ++i) {
		var oE = document.createElement('INPUT');
		oE.name = arguments[i]['name'];
		oE.value = arguments[i]['value'];
		oForm.appendChild(oE);
	}
	
	oForm.submit();
}

function verify(url, msg) {
	if (confirm(msg)) {
		go(url);
	}
}


if (!window.sea) 
{
	window.sea = new Object();
}

/**********************************************************************

	Timer
		callEvery(sec)
		callOnceAfter(sec)
		cancel()

**********************************************************************/
sea.Timer = function(callBack)
{
	this.callBack = callBack;
	this.intervalId = NaN;
	this.timeoutId = NaN;
}

sea.Timer.prototype.callEvery = function(sec)
{
	with (this) {
		cancel();
		intervalId = setInterval(callBack, sec*1000);
	}
}

sea.Timer.prototype.callOnceAfter = function(sec)
{
	with (this) {
		cancel();
		timeoutId = setTimeout(callBack, sec*1000);
	}
}

sea.Timer.prototype.cancel = function()
{
	with (this) {
		if (!isNaN(intervalId)) {
			clearInterval(intervalId);
			intervalId = NaN;
		}
		if (!isNaN(timeoutId)) {
			clearTimeout(timeoutId);
			timeoutId = NaN;
		}
	}
}



/**********************************************************************

	Navigator
	
**********************************************************************/
sea.Navigator = function()
{
}

sea.Navigator.prototype.language = function()
{
	if (!window.navigator.userLanguage) {
		return window.navigator.language;	//Mozilla
	}
	return window.navigator.userLanguage;
}

sea.Navigator.prototype.getName = function()
{
	return window.navigator.appName;
}

sea.Navigator.prototype.getVersion = function()
{
	var ver = new String(window.navigator.appVersion);
	if (this.isExplorer()) {
		var found = ver.match('MSIE (\\d+).\\d+;');
		return new Number(found[1]);
	}
	else if (this.isMozilla()) {
		var found = ver.match('^(\\d+).');
		return new Number(found[1]);
	}
	return 0;
}

sea.Navigator.prototype.isExplorer = function()
{
	return this.getName() == 'Microsoft Internet Explorer';
}

sea.Navigator.prototype.isMozilla = function()
{
	var sAgent = new String(window.navigator.userAgent);
	return sAgent.match('Firefox') != null;
}



/**********************************************************************

	Tabs
	
**********************************************************************/
sea.Tabs = function(oButtonsHolder, oTabsHolder) 
{
	this.name = '';
	this.oHolder = oButtonsHolder;
	this.oTabsHolder = oTabsHolder;
	this.activeTab = 0;
	this.tabs = new Array;
}

sea.Tabs.prototype.add = function(name, obj)
{
	this.tabs.push( {'name':name, 'obj':obj} );
	this.removeChildNode(this.oTabsHolder, obj);
}

sea.Tabs.prototype.create = function()
{
	for (nm in window) {
		if (window[nm] == this) {
			this.name = nm;
			break;
		}
	}
	
	if (this.tabs.length) {
		this.renderButtons();
		this.showActiveTab();
	}
}

sea.Tabs.prototype.goTab = function(iTab)
{
	this.hideActiveTab();
	this.activeTab = iTab;
	this.showActiveTab();
}

sea.Tabs.prototype.hideActiveTab = function()
{
	document.getElementById('tabButton' + this.activeTab).className = 'inact';
	this.removeChildNode(this.oTabsHolder, this.tabs[this.activeTab].obj);
}

sea.Tabs.prototype.showActiveTab = function()
{
	document.getElementById('tabButton' + this.activeTab).className = 'act';
	this.appendChildNode(this.oTabsHolder, this.tabs[this.activeTab].obj);
}

sea.Tabs.prototype.renderButtons = function()
{
	var txt =
		"<table id=tabNav>" +
			"<tr>" +
				"<td id=left>&nbsp;</td>";
		
	for (var i in this.tabs) {
		txt += (
			"<td class=inact id='tabButton" + i + "'>" +
				"<a href=javascript:" + this.name + '.goTab(' + i + ')>' +
					this.tabs[i].name +
				"</a>" +
			"</td>"
		);
	}		
	
	txt += 
				"<td width=100% id=right></td>";
			"</tr>" +
		"</table>";
		
	this.oHolder.innerHTML = txt;
}

sea.Tabs.prototype.removeChildNode = function(oParent, oChild) 
{
	var nav = new sea.Navigator;
	if (nav.isExplorer() && nav.getVersion() < 7) {
		var oClone = oChild.cloneNode(true);			//due to bug in IE6
		this.copyInputStates(oChild, oClone);
		oParent.removeChild(oChild);
		this.copyInputStates(oClone, oChild);
	}
	else {
		oParent.removeChild(oChild);
	}
}

sea.Tabs.prototype.appendChildNode = function(oParent, oChild) 
{
	var nav = new sea.Navigator;
	if (nav.isExplorer() && nav.getVersion() < 7) {
		var oClone = oChild.cloneNode(true);			//due to bug in IE6
		this.copyInputStates(oChild, oClone);
		oParent.appendChild(oChild);
		this.copyInputStates(oClone, oChild);
	}
	else {
		oParent.appendChild(oChild);
	}
}

sea.Tabs.prototype.copyInputStates = function(oFrom, oTo) 
{
	var oInputsFrom = oFrom.getElementsByTagName('INPUT');
	var oInputsTo = oTo.getElementsByTagName('INPUT');
	for (var i=0; i<oInputsFrom.length; ++i) {
		if ('checkbox' == oInputsTo[i].type) {
			oInputsTo[i].checked = oInputsFrom[i].checked;
		}
	}
}



/**********************************************************************

										Slider

<style>
table.slider
{
}

td.slider_screen
{
}

td.slider_screen img
{
}

td.slider_ruler
{
}

a.slider_button
{
}

a.slider_button_active
{
}
</style>

var slider = new sea.Slider(document.getElementById(''), 100, 100, 12);
slider.addImage('img_small.jpg','img_large.jpg');
...
slider.create();
**********************************************************************/

sea.Slider = function(owner, width, height, effectType) 
{ 
	this.images = new Array();
	this.width = width;
	this.height = height;
	this.active = null;
	
	var topEl = document.createElement('SPAN');
	
	topEl.innerHTML = "																					\
		<table class=slider style='border-collapse:collapse; table-layout:fixed;'>		\
			<tr>																								\
				<td align=center class=slider_screen>												\
					<a>																						\
						<img border=0>																		\
					</a>																						\
				</td>																							\
			</tr>																								\
			<tr>																								\
				<td align=center class=slider_ruler>												\
				</td>																							\
			</tr>																								\
		</table>																								\
	";
	
	topEl.getElementsByTagName('TABLE')[0].getElementsByTagName('TD')[0].width = width;
	topEl.getElementsByTagName('TABLE')[0].getElementsByTagName('TD')[0].height = height;
	
	this.link = topEl.getElementsByTagName('A')[0];
	this.screen = topEl.getElementsByTagName('IMG')[0];
	
	if (effectType) {
		this.screen.style.filter = 'revealTrans(duration=2, transition=' + effectType + ')';
	}
	
	this.ruler = topEl.getElementsByTagName('TABLE')[0].getElementsByTagName('TD')[1];
	
	owner.appendChild(topEl);
	
	this.name = 'sea_slider_N_' + (++sea.Slider.prototype.instances);
	window[this.name] = this;
}

sea.Slider.prototype.addImage = function(thumbnail, full, width, height, title)
{
	this.images.push(
		{
			'thumbnail' : thumbnail,
			'full' : full,
			'width' : width,
			'height' : height,
			'title' : title
		}
	);

	this.ruler.innerHTML += 
		'<a class=slider_button href=javascript:' + this.name + '.go(' + 
			(this.images.length-1) + ')>' + this.images.length + '</a>&nbsp;';

}

sea.Slider.prototype.create = function()
{
	if (this.images.length < 2) 
	{
		this.ruler.innerHTML = '';
	}
	if (this.images.length) {
		this.go(0);
	}
}

sea.Slider.prototype.go = function(iImg)			//zero based 
{
	if (this.screen.filters && this.screen.filters.length) {
		this.screen.filters[0].Apply();
	}
	
	if (this.images.length > 1)
	{
		var buttons = this.ruler.getElementsByTagName('A');
		if (this.active != null) {
			buttons[this.active].className = 'slider_button';
		}
		this.active = iImg;
		
		buttons[this.active].className = 'slider_button_active';
	}
	
	this.screen.src = this.images[iImg].thumbnail;
	
	if (this.images[iImg].width) {
		this.screen.width = this.images[iImg].width;
	}
	else {
		this.screen.width = this.width;
	}
	
	if (this.images[iImg].height) {
		this.screen.height = this.images[iImg].height;
	}
	else {
		this.screen.height = this.height;
	}
	
	if (this.images[iImg].title) {
		this.screen.title = this.images[iImg].title;
		this.screen.alt = this.images[iImg].title;
	}
		
	this.link.href = "																	\
		javascript:	void 																	\
			window.open('" + this.images[iImg].full + "',				\
				'_blank',																	\
				'location=no,menubar=no,status=no,toolbar=no,resizable=yes'	\
			);																					\
	";
	
	if (this.screen.filters && this.screen.filters.length) {
		this.screen.filters[0].Play();
	}
}


// private -----------------------------------------------------------------

sea.Slider.prototype.instances = 0;






/**********************************************************************

   HTTPRequest passes object of HTTPResponse type to onComplete function   
   
***********************************************************************/
sea.HTTPResponse = function(oHttpRequest)
{
    this.oReq = oHttpRequest;
}

sea.HTTPResponse.prototype.asText = function()
{
    return this.oReq.responseText;
}

sea.HTTPResponse.prototype.asXML = function()
{
    var oXml = this.oReq.responseXML;
    if (oXml.parseError) {
	if (oXml.parseError.errorCode != 0) {
	    throw new Error("XML parse error: " + oXml.parseError.reason);
	}
    }
    return oXml;
}



sea.HTTPRequest = function(onComplete, onError, method)
{
    this.method = (method==null) ? 'POST' : method;
    
    this.REQUEST_STATE_UNINITIALIZED = 0;
    this.REQUEST_STATE_OPEN = 1;
    this.REQUEST_STATE_SENT = 2;
    this.REQUEST_STATE_RECEIVING = 3;
    this.REQUEST_STATE_LOADED = 4;

    this.onComplete = onComplete;
    this.onError = onError;
}

sea.HTTPRequest.prototype.send = function(url, params)
{
    this.oReq = this.create();
    if (null == this.oReq) {
        throw new Error('Can not create instance of XMLHttpHTTPRequest');
    }
    this.oReq.onreadystatechange =
        function (ob) {
	    return function() {ob.onChangeState()};
	} (this);


    if ('GET' == this.method) {
	this.get(url, params);
    }
    else {
	this.post(url, params);
    }
}


// private -----------------------------------------------------------------

sea.HTTPRequest.prototype.get = function(url, params)
{
    var uri = this.convertParamsToURI(params);
    if (uri.length) {
        url += ('?' + uri);
    }
    this.oReq.open('GET',url,true);
    this.oReq.send(null);
}

sea.HTTPRequest.prototype.post = function(url, params)
{
    var uri = this.convertParamsToURI(params);
    this.oReq.open('POST',url,true);
    this.oReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    this.oReq.setRequestHeader('Content-length', uri.length);
    this.oReq.setRequestHeader('Connection', 'close');
    this.oReq.send(uri);
}

sea.HTTPRequest.prototype.onChangeState = function()
{
    if (this.oReq.readyState == this.REQUEST_STATE_LOADED) {
	if (this.oReq.status == 200) {
	    if (this.onComplete) {
		this.onComplete(new sea.HTTPResponse(this.oReq));
	    }
	}
	else {
	    if (this.onError) {
		this.onError();
	    }
	    else {
		throw new Error(
				'XMLHttpRequest server error: ' + 
				this.oReq.statusText
				);
	    }
	}
    }
}

sea.HTTPRequest.prototype.convertParamsToURI = function(params)
{
    var uri = '';
    for (var nm in params) {
        if (uri.length) {
            uri += '&';
        }
        uri += (nm + '=' + encodeURIComponent(params[nm]));
    }
    return uri;
}

sea.HTTPRequest.prototype.create = function()
{
    try {
		return new XMLHttpRequest();
    }
    catch (e) {}

    var ob = null;
    var versions = new Array(
			     'MSXML2.XMLHTTP.6.0',
			     'MSXML2.XMLHTTP.5.0',
			     'MSXML2.XMLHTTP.4.0',
			     'MSXML2.XMLHTTP.3.0',
			     'MSXML2.XMLHTTP',
			     'Microsoft.XMLHTTP'
			     );
    for (var i=0; i<versions.length && !ob; ++i) 
    {
		try {
		    ob = new ActiveXObject(versions[i]);
		}
		catch (e) {}
    }

    return ob;
}



/**
*  String functions
*/

sea.trim = function(str, chars) 
{
	return sea.ltrim(sea.rtrim(str, chars), chars);
}
 
sea.ltrim = function(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
sea.rtrim = function(str, chars) 
{
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

